package cn.itsource.pethome.detp.controller;

import cn.itsource.pethome.base.util.AjaxResult;
import cn.itsource.pethome.base.util.PageList;
import cn.itsource.pethome.detp.domian.Department;
import cn.itsource.pethome.detp.query.DepartmentQuery;
import cn.itsource.pethome.detp.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
//@ResponseBody//ResponseBody加载类上 当前类的所有方法返回都是json数据格式

@RestController//==Controller+ResponseBody
@RequestMapping("/dept")//我们当前的跟目录
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /*@RequestMapping("/findByDeptId")
    public Department findByDeptId(Long id){

    }*/

    //http://localhost:8080/dept/1
    //@RequestMapping(value = "/{id}",method = RequestMethod.GET)


    //下面就是我们要掌握的方式

    /**
     * 查询一个部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryDept(@PathVariable("id") Long id){
       return departmentService.findByDeptId(id);
    }

    /**
     * 删除一个部门
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult dept(@PathVariable("id")Long id){
        try {
            departmentService.delDeptById(id);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Department> queryDepts(@RequestBody DepartmentQuery query){
        return departmentService.findAll(query);
    }
    /**
     * 获取部门树
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取部门树" )
    public List<Department> dept(){
        return departmentService.getTree();
    }

    /**
     * 修改或者添加
     * @param department
     */
    @PutMapping
    @ApiOperation(value = "修改或者是添加,如果传了id就修改" )
    public AjaxResult detp(@RequestBody Department department){
        try {
            if(department.getId() != null){
                departmentService.uptDeptById(department);
            }else {
                departmentService.addDept(department);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
     * PatchMapping这个就是用来做批量操作
     */
    @PatchMapping
    @ApiOperation(value = "部门的批量删除")
    public AjaxResult dept(@RequestBody List<Integer> ids){
        try {
            departmentService.batchDelDept(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
